package cn.tedu.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.CountDownLatch;

public class ZookeeperDemo2 {
    private ZooKeeper zooKeeper;
    final private String ipAndPort = "mycentos8:2181";

    @Before
    public void connect() throws IOException, InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(ipAndPort, 5000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("连接成功");
                }
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
    }

    /**
     * 监控Zookeeper中/log节点的数据是否发生变化
     */
    @Test
    public void dataChanged() throws KeeperException, InterruptedException {
        // get过程本身是非阻塞的，就意味着可能还没有监控到变化，get过程就结束了
        CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper.getData("/log", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                    System.out.println("节点数据被修改了");
                    System.out.println("修改时间: " + new Date(System.currentTimeMillis()));
                    System.out.println(watchedEvent.getState());
                }
                countDownLatch.countDown();
            }
        }, null);
        countDownLatch.await();
    }

    /**
     * 监控子节点个数变化
     */
    @Test
    public void childrenChanged() throws KeeperException, InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper.getChildren("/log", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
                    System.out.println("子节点个数发生变化");
                    System.out.println("变化时间: " + new Date(System.currentTimeMillis()));
                }
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
    }

    /**
     * 监控节点的创建或删除
     */
    @Test
    public void nodeChanged() throws KeeperException, InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper.exists("/tmp", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getType() == Event.EventType.NodeCreated) {
                    System.out.println("新节点被创建");
                    System.out.println("创建时间: " + new Date(System.currentTimeMillis()));
                } else if (watchedEvent.getType() == Event.EventType.NodeDeleted) {
                    System.out.println("节点被删除");
                    System.out.println("删除时间: " + new Date(System.currentTimeMillis()));
                }
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
    }
}
